import { intersect, highlightByGeometry, getBaseData } from 'lark-map'
import QueryUtils from '@/utils/yc/QueryUtils'
import GeometryUtils from '@/utils/yc/GeometryUtils'
import Utils from '@/utils/yc/Utils'
import SymbolUtils from '@/utils/yc/SymbolUtils'
import { format } from '@/utils/yc/format'

/**
 * 生态坐标体检
 */
export default {
  data() {
    return {

      /**
       * 查询数据配置
       */
      queryConfig: [
        {
          type: 'GridQueryService', name: '气象数据'
        },
        {
          type: 'GetFeaturesByGeometryService',
          name: '土壤数据'
        },
        {
          type: 'GetFeaturesByGeometryService', name: '植被数据'
        },
        {
          type: 'GetFeaturesByGeometryService', name: '修复技术'
        }
      ],
      //返回结果集合
      resultObject: {
        //类型
        type: '1',
        //基础数据
        baseData: {
          jd: 0, //经度
          wd: 0,//纬度
          xzqmc: '',//行政区名称
          bh: ''//编号
        },
        //气象特征
        qxtz: {
          ljjy: '350ml~400ml',
          pjsd: '2°C~6°C',
          pjwd: '2°C~6°C',
          yxjw: '2°C~6°C',
          pjrzss: '124天26小时~128天17小时',
          dfts: '22天~28天',
          ghts: '22天~28天'
        },
        //地理特征
        dltz: {
          pd: '45°C~60°C',
          px: '0~360',
          hbgd: '1100m~1500m',
          styy: '0~10'
        },
        //土壤特征
        trtz: {
          trlx: '黑钙土',
          trsd: '20%~30%'
        },
        //集合图形
        features: []
      }

    }
  },
  methods: {

    /**
     * 点查询数据
     * @param {*} view    地图
     * @param {*} feature 行政区图形
     * @param {*} point   点图形
     */
    async queryDistrictByCoord(view, feature, point) {
      if (view === null || feature === null || point === null) return
      let geometry = intersect({ geometry: feature.geometry, intersector: point })
      let owner = this
      if (geometry) {
        //查询数据
        getBaseData({ geometry: geometry, queryConfig: this.queryConfig })
          .then(async res => {
            let result = await format(res, view, geometry)
            if (result) {
              //地理特征
              owner.resultObject.qxtz = result.qxtz
              //气象特征
              owner.resultObject.dltz = result.dltz
              //土壤特征
              owner.resultObject.trtz = result.trtz
            }
          })
        //中心点渲染
        highlightByGeometry({ view, geometry: point, symbol: SymbolUtils.LOCATION_PICTURE })
        let res = await QueryUtils.queryByGeometry('xzq_point', view, geometry)
        if (res && res.length > 0) {
          let features = res[0]['results']
          let graphic = features[0]['feature']
          let name = graphic.attributes.NAME
          let code = graphic.attributes.CODE
          let objectId = graphic.attributes.OBJECTID
          let fileName = Utils.getFileName(code, objectId)
          let geoJson = GeometryUtils.graphicToGeoJson([graphic])
          //设置基础数据
          let x = geometry.longitude.toFixed(4)
          let y = geometry.latitude.toFixed(4)
          this.resultObject.baseData.jd = x === undefined ? 0 : x
          this.resultObject.baseData.wd = y === undefined ? 0 : y
          this.resultObject.baseData.xzqmc = feature.attributes.NAME + name
          this.resultObject.baseData.bh = fileName
          //设置图形数据
          let sfeature = {
            fileName: fileName,
            jsonFeature: JSON.stringify(geoJson)
          }
          this.resultObject.features = [sfeature]
          return this.resultObject
        }
      }
    }
  }
}
